(() => {
	let fn = Element.prototype.draw;
	Element.prototype.draw = function(callback) {
		fn && fn();
		let me = this;
		if (!me.nodeType || !me.tagName || me.tagName.toLowerCase() !== 'canvas') {
			return;
		}
		let ctx = me.getContext('2d');
		if(!ctx) {
			return;
		}
		let paint = {};
		for (let key in ctx) {
			paint[key] = (function () {
				let ctx = this.ctx;
				let key = this.key;
				let params = [];
				let _arguments = Array.prototype.slice.apply(arguments);
				_arguments.forEach(function (arg) {
					if (typeof arg === 'function') {
						arg = arg.call(me, ctx);
						if (typeof arg === 'object' && !!arg.slice) {
							params = params.concat(arg);
						} else {
							params.push(arg);
						}
					} else {
						params.push(arg);
					}
				});

				if (typeof ctx[key] === 'function') {
					ctx[key].apply(ctx, params);
				} else {
					ctx[key] = params[0];
				}
				return paint;
			}).bind({
				ctx : ctx,
				key : key
			});
		}
		// 函数修正
		paint.measureText = function(...args) {
			return ctx.measureText(...args);
		}
		paint.createLinearGradient = function(...args) {
			return ctx.createLinearGradient(...args);
		}
		paint.lineWidth = function(width) {
			if(width) {
				ctx.lineWidth = width;
				return this;
			}
			return ctx.lineWidth;
		}
		paint.isPointInPath = function(...args) {
			return ctx.isPointInPath(...args);
		}
		paint.isPointInStroke = function(...args) {
			return ctx.isPointInStroke(...args);
		}
		if (typeof callback === 'function') {
			callback.call(ctx, paint);
		}
		return ctx;
	};
})();
